package cn.djb.day17;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class MedianFinder {

    private List<Integer> nums;

    /**
     * initialize your data structure here.
     */
    public MedianFinder() {
        this.nums = new ArrayList<>();
    }

    public void addNum(int num) {
        this.nums.add(num);
    }

    public double findMedian() {
        int size = this.nums.size();
        if (size == 0) return 0;
        Collections.sort(this.nums);
        if (size % 2 == 0) {
            //偶数
            int middle = size / 2;
            return (double)(this.nums.get(middle) + this.nums.get(middle - 1)) / 2;
        }else {
            //奇数
            int middle = size / 2;
            return this.nums.get(middle);
        }
    }

    public static void main(String[] args) {
        MedianFinder finder = new MedianFinder();
        finder.addNum(1);
        finder.addNum(2);
        System.out.println(finder.findMedian());
    }
}